<?php

/**
 * Array for build path for current_item.
 */
$paths = array();

/**
 * Callack for xml_set_element_handler.
 *
 * @param $parser reference to the XML parser calling the handler
 * @param $name string  contains the name of the element
 *  for which this handler is called
 * @param $attrs contains an associative array with the element's attributes
 */
function startElement($parser, $name, $attrs)
{
    global $depth;
    global $counter;
    global $items;
    global $paths;

    $counter++;
    $depth++;

    $current_item = new xml_item();
    $current_item->depth = $depth;
    $current_item->name = $name;
    $current_item->data = "";
    $current_item->attrs = $attrs;

    $paths[$depth] = $name;

    $path = "";
    for ($i = 1; $i <= $depth; $i++)
    {
        if ($path != "")
            $path .= "/";
        $path .=$paths[$i];
    }
    $current_item->path = $path;
    $items[$path] = $current_item;
}

/**
 * Callack for xml_set_element_handler.
 *
 * @param $parser reference to the XML parser calling the handler
 * @param $name string  contains the name of the element
 *  for which this handler is called
 */
function endElement($parser, $name)
{
    global $depth;

    $depth--;
}

/**
 * Callback for xml_set_character_data_handler
 *
 * @param $parser reference to the XML parser calling the handler
 * @param $data string characters data
 */
function characterData($parser, $data)
{
    global $depth;
    global $counter;
    global $items;
    global $paths;

    $path = "";
    for ($i = 1; $i <= $depth; $i++)
    {
        if ($path != "")
            $path .= "/";
        $path .=$paths[$i];
    }

    $items[$path]->data .= $data;
}

/**
 * Get data from xml
 *
 * @param $url string xml file
 *
 * @return array data array
 */
function get_xml_data($url)
{
    global $depth;
    global $counter;
    global $items;
    global $encoding;
    global $version;

    $depth = 0;
    $counter = 0;
    $items = array();
    $version = substr(phpversion(),0,1);

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser,
                "startElement",
                "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");

    $body = file_get_contents($url);

    if ($version<5)
    {
        $subject = $body;

        $pattern = "/encoding([ ]*)=([ ]*)(\'|\")([0-9a-zA-Z-]+)(\'|\")/";
        $result = preg_match($pattern, $subject, $matches,
                    PREG_OFFSET_CAPTURE, 3);

        if ($result == 0)
            $encoding = "UTF-8";
        else
        {
            $encoding = $matches[4][0];
            $body = iconv($encoding, "UTF-8", $body);
        }
    }

    if (!xml_parse($xml_parser, $body))
    {
        return array();
    }

    xml_parser_free($xml_parser);

    $data = array();
    foreach($items as $key => $item)
    {
        $data[$key] = $item->data;
    }

    return $data;
}

